package com.SFA.OrderManagement;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.FrameWork.Entity.OrderEntity;
import com.FrameWork.Utils.DateTimeUtils;
import com.SFA.R;
import com.SFA.Customer.CustomerManager;

public class OrderAdapter extends BaseAdapter implements OnItemClickListener, OnClickListener{

	private List<OrderEntity> list = null;
	private LayoutInflater inflater;
	private Context context = null;
	public OrderAdapter(List<OrderEntity> list,Context context)
	{
		this.list= list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list != null)
			return list.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(list != null && list.size()-1>=position)
			return list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHoder hoder = null;
		if (convertView == null)
		{

			hoder = new ViewHoder();
			convertView = inflater.inflate(R.layout.orderlistviewitem, null);
			hoder.Amount = (TextView) convertView.findViewById(R.id.tvAmount);
			hoder.Date = (TextView) convertView.findViewById(R.id.tvOrderDate);
			hoder.Status = (TextView) convertView.findViewById(R.id.tvStatus);
			hoder.Type = (TextView) convertView.findViewById(R.id.tvType);
			hoder.Tel=(TextView) convertView.findViewById(R.id.tvContactTel);
			hoder.Address=(TextView) convertView.findViewById(R.id.tvContactAddress);
			hoder.OrderNO= (TextView) convertView.findViewById(R.id.tvOrderNO);
			hoder.ContactName=(TextView) convertView.findViewById(R.id.tvContactName);
			hoder.btnEdit=(Button) convertView.findViewById(R.id.btnEdit);
			hoder.btnCopy=(Button) convertView.findViewById(R.id.btnCopy);
			convertView.setTag(hoder);
		}
		else
		{
			hoder = (ViewHoder) convertView.getTag();
		}
		OrderEntity entity = (OrderEntity)getItem(position);

		DecimalFormat fnum = new DecimalFormat("##0.00"); 
		String dd=fnum.format(entity.TOTAL_AMOUNT-entity.DISC_AMOUNT); 

		hoder.Amount.setText(dd + "元");
		hoder.Date.setText(DateTimeUtils.getSelects12Date(entity.ORDER_DATE));
		hoder.Address.setText(entity.DELIVERY_ADDRESS);
		hoder.Tel.setText(entity.CONTACT_LINK);
		hoder.OrderNO.setText(entity.ORDER_NO);
		hoder.ContactName.setText(entity.CONTACT_MAN);
		if("0".equals(entity.STATUS))
		{
			hoder.Status.setText("已下单");
		}
		else if("2".equals(entity.STATUS))
		{
			hoder.Status.setText("已确认");
		}
		else if("20".equals(entity.STATUS))
		{
			hoder.Status.setText("部份确认");
		}
		else if("1".equals(entity.STATUS))
		{
			hoder.Status.setText("已取消");
		}
		else if("10".equals(entity.STATUS))
		{
			hoder.Status.setText("部分已取消");
		}
		else if("3".equals(entity.STATUS))
		{
			hoder.Status.setText("已制作");
		}
		else if("30".equals(entity.STATUS))
		{
			hoder.Status.setText("部份已制作");
		}
		else if("4".equals(entity.STATUS)){
			hoder.Status.setText("已完成[未提货]");
		}
		else if("40".equals(entity.STATUS))
		{
			hoder.Status.setText("部份已完成[未提货]");
		}
		else if("5".equals(entity.STATUS)){
			hoder.Status.setText("已提货");
		}
		else if("50".equals(entity.STATUS)){
			hoder.Status.setText("部分已提货");
		}

		if("2".equals(entity.ORDERTYPE))
		{
			hoder.Type.setText("普通单");
		}
		else if("1".equals(entity.ORDERTYPE))
		{
			hoder.Type.setText("赠品单");
		}

		hoder.btnCopy.setOnClickListener(this);
		hoder.btnCopy.setTag(entity);
		hoder.btnEdit.setOnClickListener(this);
		hoder.btnEdit.setTag(entity);
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Intent intent= new Intent(context, OrderDetailActivity.class);
		((Activity)context).startActivityForResult(intent,100);
		((Activity)context).overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
	}

	class ViewHoder
	{
		private TextView Amount;
		private TextView Date;
		private TextView Status;
		private TextView Type;
		private TextView Tel;
		private TextView Address;
		private TextView ContactName;
		private TextView OrderNO;

		private Button btnCopy;
		private Button btnEdit;
	}

	@Override
	public void onClick(View v) {
		OrderEntity entity=(OrderEntity) v.getTag();
		Intent intent= new Intent(context, OrderDetailActivity.class);
		OrderManager manager = new OrderManager();
		entity.Lines=manager.getOrderLineListByOrderNo(context, entity.ORDER_NO);

		if(v.getId() == R.id.btnEdit)  //修改订单
		{
			entity.paymentlist=manager.getAllPaymentByOrderId(context, entity.ID);
		}
		else //复制订单，但不复制订单内的产品
		{
			entity.ID=null;
			entity.DISC_AMOUNT=0;
		}
		intent.putExtra("ORDER", entity);
		intent.putExtra("CUSTOMER", new CustomerManager(context).getCustomerInfo(entity.CUSTOMER_ID));
		((Activity)context).startActivityForResult(intent,100);
		((Activity)context).overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);



	}

}
